import { ref } from "vue"
import { defineStore } from "pinia"
import { getUserInfoService } from "@/api/user"

const useUserStore = defineStore('big-user', () => {
  const userInfo = ref({
    token: ''
  })

  const user = ref({
    id: '',
    username: '',
    nickname: '',
    email: '',
    user_pic: ''
  })

  // 获取用户信息
  const getUserInfo = async () => {
    const res = await getUserInfoService()
    user.value = res.data.data

  }
  // 设置token方法
  const setToken = (token) => {
    userInfo.value.token = token
  }
  // 移除token方法
  const removeToken = () => {
    userInfo.value.token = ''
  }
  return {
    userInfo,
    getUserInfo,
    setToken,
    removeToken,
    user
  }
}, {
  persist: {
    key: 'token',
    pick: ['userInfo.token']
  }
})

export default useUserStore
